import AntModal from '@/components/pt/dialog/AntModal'
import { getPlatformConfig, addPlatformConfig, updatePlatformConfig, checkCodeUnique } from '@/api/platform/platformConfig'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    AntModal
  },
  data() {
    const validateCode = (rule, value, callback) => {
      checkCodeUnique(this.form.id, this.form.key).then(response => {
        if (response.data) {
          return callback(new Error('键不能重复'))
        }
        callback()
      })
    }
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加平台配置',
      // 表单参数
      form: {},
      rules: {
        key: [
          { required: true, message: '键不能为空' },
          { validator: validateCode, trigger: 'blur' }
        ],
        valueString: [
          { required: true, message: '字符串值不能为空' },
        ]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        key: undefined,
        valueString: undefined,
        valueRich: undefined,
        explain: undefined,
        type: '1',
        status: '0'
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加平台配置'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const platformConfigId = row.id
      getPlatformConfig(platformConfigId).then(response => {
        this.form = response.data
        this.formTitle = '修改平台配置'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            updatePlatformConfig(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addPlatformConfig(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/platformconfig/index'
      this.$router.push(index)
    }
  }
}
